/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cadi.games;

import com.cadi.aux.Window;
import com.cadi.aux.Coin;
import static com.cadi.aux.Window.styleText;
import com.cadi.menu.MainMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;

/**
 *
 * @author ApoZuniga
 */
public class SpecialCounter extends Window {
    private JLabel instruction;
    private JLabel requiredTotal;
    private JLabel labelTotal;
    private JLabel userTotal;
    protected Coin objectOne;
    protected Coin objectTwo;
    protected Coin objectThree;
    protected Coin objectFour;
    protected Coin objectFive;
    protected JButton optionTotalOne;
    protected JButton optionTotalTwo;
    protected JButton optionTotalThree;
    protected int valueOptionOne;
    protected int valueOptionTwo;
    protected int valueOptionThree;
    private int total;
    private int initialMoney;
    protected ActionListener valueListenerOne;
    protected ActionListener valueListenerTwo;
    protected ActionListener valueListenerThree;
    protected int currentLevel;
    private JLabel background;

    public SpecialCounter() {
        total = 35;
        initialMoney = 27;
        instruction = new JLabel("Selecciona las monedas y/o billetes necesarios para formar la siguiente cantidad");
        instruction.setFont(styleText);
        instruction.setBounds(50, -50, 800, 200);
        requiredTotal = new JLabel("$ 35");
        requiredTotal.setFont(styleMoney);
        requiredTotal.setBounds(300, -30, 500, 300);
        labelTotal = new JLabel("Llevas acumulados: ");
        labelTotal.setFont(styleText);
        labelTotal.setBounds(270, 455, 200, 150);
        userTotal = new JLabel("$ " + String.valueOf(initialMoney));
        userTotal.setFont(styleMoney);
        userTotal.setBounds(500, 420, 300, 200);
        container.add(userTotal, JLayeredPane.PALETTE_LAYER);
        container.add(labelTotal, JLayeredPane.PALETTE_LAYER);
        container.add(requiredTotal, JLayeredPane.PALETTE_LAYER);
        container.add(instruction, JLayeredPane.PALETTE_LAYER);
        
        toMenu.setVisible(true);
        currentLevel = 1;
        
        valueListenerOne = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                calculateTotalActionPerformed(ae, valueOptionOne);
            }
        };
        valueListenerTwo = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                calculateTotalActionPerformed(ae, valueOptionTwo);
            }
        };
        valueListenerThree = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                calculateTotalActionPerformed(ae, valueOptionThree);
            }
        };
        objectOne = new Coin(20, "large");
        objectOne.setBounds(250, 200, 150, 100); //x, y, w, h
        container.add(objectOne, JLayeredPane.PALETTE_LAYER);
        objectTwo = new Coin(5, "small");
        objectTwo.setBounds(250, 320, 80, 81);
       container.add(objectTwo, JLayeredPane.PALETTE_LAYER);
        objectThree = new Coin(1, "small");
        objectThree.setBounds(520, 200, 75, 75);
        container.add(objectThree, JLayeredPane.PALETTE_LAYER);
        objectFour = new Coin(1, "small");
        objectFour.setBounds(520, 320, 75, 75);
        container.add(objectFour, JLayeredPane.PALETTE_LAYER);
        objectFive = new Coin(5, "small");
        objectFive.setBounds(550, 320, 75, 75);
        container.add(objectFive, JLayeredPane.PALETTE_LAYER);
        objectFive.setVisible(false);
        
        optionTotalOne = new JButton(new ImageIcon(getClass().getResource("/resources/1peso-small.png")));
        optionTotalOne.setBounds(900, 155, 100, 100);
        valueOptionOne = 1;
        optionTotalOne.addActionListener(valueListenerOne);
        container.add(optionTotalOne, JLayeredPane.PALETTE_LAYER);
        optionTotalTwo = new JButton(new ImageIcon(getClass().getResource("/resources/2pesos-small.png")));
        optionTotalTwo.setBounds(900, 290, 100, 100);
        valueOptionTwo = 2;
        optionTotalTwo.addActionListener(valueListenerTwo);
        container.add(optionTotalTwo, JLayeredPane.PALETTE_LAYER);
        optionTotalThree = new JButton(new ImageIcon(getClass().getResource("/resources/5pesos-small.png")));
        optionTotalThree.setBounds(900, 425, 100, 100);
        valueOptionThree = 5;
        optionTotalThree.addActionListener(valueListenerThree);
        container.add(optionTotalThree, JLayeredPane.PALETTE_LAYER);
        background = new JLabel(new ImageIcon(getClass().getResource("/resources/fondo5.png")));
        background.setBounds(0, 0, 1280,800);
        container.add(background, JLayeredPane.PALETTE_LAYER);
        window.setVisible(true);
    }
    
     private void totalActionPerformed(ActionEvent evt, int answer) {
        if (answer == 1) {
            JOptionPane.showMessageDialog(null, "¡Bien Hecho!");
        } else {
            JOptionPane.showMessageDialog(null, "Intenta de Nuevo");
        }
    }
    
    private void calculateTotalActionPerformed(ActionEvent evt, int valueMoney) {
        initialMoney += valueMoney;
        if (initialMoney < total) {
            userTotal.setText("$ " + String.valueOf(initialMoney));
        } else if (initialMoney == total) {
            userTotal.setText("$ " + String.valueOf(initialMoney));
            result = java.applet.Applet.newAudioClip(getClass().getResource("/resources/bien.wav"));
                result.play();
            JOptionPane.showMessageDialog(null, "¡Bien Hecho!", "¡Bien Hecho!", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(getClass().getResource("/resources/right.png")));
            changeLevel();
        } else if (initialMoney > total) {
            result = java.applet.Applet.newAudioClip(getClass().getResource("/resources/mal.wav"));
                result.play();
            JOptionPane.showMessageDialog(null, "Intenta de nuevo", "Intenta de nuevo", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(getClass().getResource("/resources/wrong.png")));
            initialMoney -= valueMoney;
        }
        window.repaint();
    }
    
    private void setLevelTwo() {
        total = 57;
        initialMoney = 32;
        requiredTotal.setText("$ 57");
        userTotal.setText("$ 32");
        objectOne.setIcon(new ImageIcon(getClass().getResource("/resources/10pesos-small.png")));
        objectOne.setBounds(250, 200, 75, 75); //x, y, w, h
        objectTwo.changeIcon(10);
        objectTwo.setBounds(250, 320, 75, 75);
        objectThree.changeIcon(10);
        objectThree.setBounds(400, 200, 75, 75);
        objectFour.changeIcon(1);
        objectFour.setBounds(400, 320, 75, 75);
        objectFive.setVisible(true);
        objectFive.changeIcon(1);
        objectFive.setBounds(525, 320, 75, 75);
        
        window.repaint();
        window.setVisible(true);
    }
    
    private void setLevelThree() {
        total = 105;
        initialMoney = 50;
        requiredTotal.setText("$ 105");
        userTotal.setText("$ 50");
        objectOne.changeIcon(20);
        objectOne.setBounds(250, 200, 180, 100); //x, y, w, h
        objectTwo.changeIcon(10);
        objectTwo.setBounds(250, 350, 75, 75);
        objectThree.changeIcon(10);
        objectThree.setBounds(480, 200, 75, 75);
        objectFour.changeIcon(5);
        objectFour.setBounds(400, 350, 80, 81);
        objectFive.changeIcon(5);
        objectFive.setBounds(550, 350, 80, 81);
        
        optionTotalOne.setIcon(new ImageIcon(getClass().getResource("/resources/10pesos-small.png")));
        valueOptionOne = 10;
        optionTotalTwo.setIcon(new ImageIcon(getClass().getResource("/resources/billete20_big.jpeg")));
        optionTotalTwo.setBounds(850, 280, 200, 120);
        valueOptionTwo = 20;
        window.repaint();
        window.setVisible(true);
    }
    
    private void setLevelFour() {
        total = 570;
        initialMoney = 250;
        requiredTotal.setText("$ 570");
        userTotal.setText("$ 250");
        objectOne.changeIcon(100);
        objectOne.setBounds(250, 200, 200, 95); //x, y, w, h
        objectTwo.setIcon(new ImageIcon(getClass().getResource("/resources/billete100_big.jpg")));
        objectTwo.setBounds(510, 200, 200, 95);
        objectThree.setIcon(new ImageIcon(getClass().getResource("/resources/billete50_big.jpg")));
        objectThree.setBounds(375, 350, 180, 91);
        objectFour.setVisible(false);
        objectFive.setVisible(false);
        
        optionTotalOne.setIcon(new ImageIcon(getClass().getResource("/resources/billete100_big.jpg")));
        optionTotalOne.setBounds(850, 150, 220, 115);
        valueOptionOne = 100;
        optionTotalThree.setIcon(new ImageIcon(getClass().getResource("/resources/billete50_big.jpg")));
        optionTotalThree.setBounds(850, 450, 200, 110);
        valueOptionThree = 50;
        window.repaint();
        window.setVisible(true);
    }
    
    private void setLevelFive() {
        total = 990;
        initialMoney = 30;
        requiredTotal.setText("$ 990");
        userTotal.setText("$ 30");
        objectOne.setIcon(new ImageIcon(getClass().getResource("/resources/10pesos-small.png")));
        objectOne.setBounds(300, 250, 75, 75); //x, y, w, h
        objectTwo.changeIcon(10);
        objectTwo.setBounds(420, 250, 75, 75);
        objectThree.changeIcon(10);
        objectThree.setBounds(540, 250, 75, 75);
        objectFour.setVisible(false);
        objectFive.setVisible(false);
        
        window.repaint();
        window.setVisible(true);
    }
    
    private void changeLevel() {
        switch(currentLevel){
            case 1:
                setLevelTwo();
                currentLevel++;
                break;
            case 2:
                setLevelThree();
                currentLevel++;
                break;
            case 3:
                setLevelFour();
                currentLevel++;
                break;
            case 4:
                setLevelFive();
                currentLevel++;
                break;
            case 5:
                MainMenu main = new MainMenu();
                window.setVisible(false);
                break;
        }
    }
}
